package com.loong.shop.business;

import com.loong.common.mybatis.business.AbstractBusiness;
import com.loong.common.rabbitmq.dto.GoodsBrandDTO;
import com.loong.common.rabbitmq.dto.base.RepositoryEventDto;
import com.loong.common.utils.TransformUtils;
import com.loong.shop.common.backend.param.GoodsBrandParam;
import com.loong.shop.model.GoodsBrand;
import com.loong.shop.mq.GoodsBrandReport;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Author: 刘群
 * @Date: 2020/10/27 7:47
 */
@Slf4j
@Repository
public class GoodsBrandBusiness extends AbstractBusiness<GoodsBrand> {

    @Resource
    private GoodsBrandReport goodsBrandReport;

    /**
     * 根据参数查询商品品牌，该方法将用于判断是否存在两个及以上品牌名称一样的商品品牌
     *
     * @param param 查询参数：品牌名称、商品类别
     * @return 商品品牌集合
     */
    public List<GoodsBrand> findGoodsBrandByParam(GoodsBrandParam param) {
        Condition condition = new Condition(GoodsBrand.class);
        Example.Criteria criteria = condition.createCriteria();
        if (ObjectUtils.isEmpty(param.getBrandName())) return null;
        //根据品牌名称和商品类别查询商品品牌
        criteria.andEqualTo("brandName", param.getBrandName());
        return this.findByCondition(condition);
    }

    /**
     * 根据商品类别查询品牌名称
     *
     * @param categoryId 商品类别
     * @return 商品品牌集合
     */
    public List<GoodsBrand> findGoodsBrandByCategoryId(Long categoryId) {
        Condition condition = new Condition(GoodsBrand.class);
        Example.Criteria criteria = condition.createCriteria();
        //根据商品类别查询商品品牌
        criteria.andEqualTo("categoryId", categoryId);
        return this.findByCondition(condition);
    }

    @Override
    public void save(GoodsBrand model) {
        //新增商品品牌到数据库
        super.save(model);
        //上报GoodsBand到MQ中
        GoodsBrandDTO goodsBrandDTO = TransformUtils.transform(model, GoodsBrandDTO.class);
        RepositoryEventDto<GoodsBrandDTO> eventDto = RepositoryEventDto.buildWithCreate(goodsBrandDTO);
        goodsBrandReport.report(eventDto);
    }

    @Override
    public void updateByPrimaryKeySelective(GoodsBrand model) {
        //更新商品品牌到数据库
        super.updateByPrimaryKeySelective(model);
        //上报GoodsBand到MQ中
        GoodsBrandDTO goodsBrandDTO = TransformUtils.transform(model, GoodsBrandDTO.class);
        RepositoryEventDto<GoodsBrandDTO> eventDto = RepositoryEventDto.buildWithUpdate(goodsBrandDTO);
        goodsBrandReport.report(eventDto);
    }

    @Override
    public void deleteById(Object id) {
        //删除数据库中的商品品牌
        super.deleteById(id);
        //上报GoodsBand到MQ中
        GoodsBrandDTO goodsBrandDTO = GoodsBrandDTO.builder().id((Long) id).build();
        RepositoryEventDto<GoodsBrandDTO> eventDto = RepositoryEventDto.buildWithDelete(goodsBrandDTO);
        goodsBrandReport.report(eventDto);
    }
}
